<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>箭头函数与this</title>
</head>

<body>
    <script>
        // es5函数的写法
        // 声明式
        function fn() {
            console.log(this);
        }
        // fn(); 
        // 表达式
        let fn2 = function () {
            console.log(this);
        }
        // fn2(); 

        // 箭头函数的写法:更适合匿名函数
        // 只有表达式
        let fn3 = () => {
            console.log(this)
            return this;
        };
        // fn3() 
        let fn4 = (a) => this;
        let rt = fn4();
        // console.log(rt);

        const obj = {
            name: 'obj',
            that: this,
            fn1: function () {
                console.log(this);
            },
            fn2: () => console.log(this)
        }
        // obj.fn1(); 
        // obj.fn2(); 
        // console.log(obj.that); //window
        function fn0() {
            return {
                fn1: function () {
                    var obj = {

                        a: function () {
                            console.log(this)
                        },
                        b: {
                            d: function () {
                                console.log(this);
                            },
                            c: () => console.log(this)
                        }
                    }
                    console.log(this);

                    return obj;
                }
            }
        }
        fn0().fn1().b.c() //{fn1:fn}
        fn0().fn1() //{fn:fn}
        // console.log(fn0().fn1());
        /**
         * 箭头函数：更简短的函数并且不绑定this。重点：简短，没有this指针
         * this：表示函数运行时所在的环境对象，重点：函数，执行环境
         **/
    </script>
</body>

</html>